package com.neusoft.springdemo.mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;

import com.neusoft.springdemo.bean.Food;
import com.neusoft.springdemo.common.ElmException;

/**
 * 
 * @author chens
 *
 */
public class FoodMapperJdbcImpl implements FoodMapper {
	private Logger logger = Logger.getLogger(FoodMapperJdbcImpl.class);

	public FoodMapperJdbcImpl() {
		super();

		logger.info("=========  FoodMapperJdbcImpl() ==============");
	}

	@Override
	public List<Food> getList() {

		List<Food> list = new ArrayList<Food>();
		list.add(new Food(1000, "鱼香肉丝", "微辣，口感很好", 23.1, 1));
		list.add(new Food(1002, "红烧狮子头", "不辣，酥松", 25.1, 1));

		return list;
	}

	@Override
	public boolean add(Food food) throws ElmException {

		logger.info("========= 新增餐品成功  。。。=========");

		int num = new Random().nextInt(10);

		int n = 2;
		if (num % n == 0) {
			throw new ElmException("发生了数据操作异常！");
		}

		return true;
	}

	@Override
	public boolean edit(Food food) throws ElmException {
		logger.info("========= 修改餐品成功  。。。=========");

		int num = new Random().nextInt(10);

		int n = 2;
		if (num % n == 0) {
			throw new ElmException("发生了数据操作异常！");
		}

		return true;
	}

	@Override
	public boolean delete(Food food) throws ElmException {
		logger.info("========= 删除餐品成功  。。。=========");

		int num = new Random().nextInt(10);
		int n = 2;
		if (num % n == 0) {
			throw new ElmException("发生了数据操作异常！");
		}

		return true;
	}

}
